<template>
  <transition name="fade">
    <div v-show="showButton" class="back-to-top" @click="scrollToTop">
      <n-icon size="20">
        <ArrowUpOutline />
      </n-icon>
    </div>
  </transition>
</template>

<script setup>
import { ArrowUpOutline } from "@vicons/ionicons5";

const showButton = ref(false);

function scrollToTop() {
  window.scrollTo({
    top: 0,
    behavior: "smooth",
  });
}

function checkScroll() {
  showButton.value = window.scrollY > 300;
}

onMounted(() => {
  window.addEventListener("scroll", checkScroll);
});

onUnmounted(() => {
  window.removeEventListener("scroll", checkScroll);
});
</script>

<style scoped>
.back-to-top {
  position: fixed;
  bottom: 30px;
  right: 30px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background-color: var(--primary-color);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, background-color 0.3s ease;
  z-index: 100;
}

.back-to-top:hover {
  transform: translateY(-5px);
}

/* 移动端优化 */
@media (max-width: 768px) {
  .back-to-top {
    bottom: 20px;
    right: 20px;
    width: 36px;
    height: 36px;
  }
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s;
}
.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>
